package com.netwit.web.controller;

import com.netwit.common.prop.DataSourceMappersProperties;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@EnableAutoConfiguration
public class IndexController {
    private static final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private DataSourceMappersProperties properties;

    @RequestMapping("")
    public String index(Map<String, Object> map, Model model, HttpServletRequest request) {
        return "index";
    }

    @RequestMapping("/grouplist")
    public String studentGroups(Map<String, Object> map, Model model, HttpServletRequest request) {
        return "management/StudentGroupList";
    }

    @RequiresAuthentication
    @RequestMapping("/schoolist")
    public String schoolList(Map<String, Object> map, Model model, HttpServletRequest request) {
        return "management/SchoolList";
    }

    @RequestMapping("/classlist")
    public String classesList(Map<String, Object> map, Model model, HttpServletRequest request){
        return "management/ClassesList";
    }

    @RequestMapping("/test")
    public Map<String, Object> getYamlProperties() {
        log.info(properties.getMap().toString());
        return properties.getMap();
    }    

    @RequestMapping("test/{alias}")
    public Map<String, Object> getMapperByAlias(@PathVariable(value="alias") String alias) {
        Map<String, Object> obj = new LinkedHashMap<>();
        obj.put("DbTableName", getDbTableNameByAlias(alias));
        obj.put("convert", convert("aa_bbv_cd_ee"));
        obj.put("op-tre", "op-tre".startsWith("op-"));
        return obj;
    }

    @RequestMapping("mapper")
    public Map<String, Object> getMapper() {
        return this.properties.getMap();
    }

    @SuppressWarnings("unchecked")
    private String getDbTableNameByAlias(String alias) {
        Map<String, Object> map = (Map<String, Object>)properties.getMap();
        for (Object m : map.values()) {
            if (((Map<String, Object>)m).containsKey(alias)) return (String)((Map<String, Object>)m).get(alias);
        }
        return alias;
    }

    private String convert(String input) {
        log.info(input.replaceAll("\\_(.)", "%S"));
        log.info(input.replaceAll("[^_]*_(.)[^_]*", "$1_"));
        Pattern pattern = Pattern.compile("[^_]*_(.)[^_]*");
        Matcher matcher = pattern.matcher(input);
        log.info(String.format("%s 通过 %s 分解", input, "[^_]*_(.)[^_]*"));
        while (matcher.find()) {
            log.info(matcher.group());
        }
        return String.format(input.replaceAll("\\_(.)", "%S"), (Object[])input.replaceAll("[^_]*_(.)[^_]*", "$1_").split("_"));
    }

}